%  This file is part of Erlywog.

%  Erlywog is free software: you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation, either version 3 of the License, or
%  (at your option) any later version.

%  Erlywog is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.

%  You should have received a copy of the GNU General Public License
%  along with Erlywog.  If not, see <http://www.gnu.org/licenses/>.

%  Copyright Jasm Sison 2009

-module(blog_navigator_controller).
-export([
  private/0
  , index/2
  , categories/1
  , projects/1
  , last_entries/1
  , archives/1
]).

%% constants
-define(MAX_LAST_ENTRIES, 10).

private () -> true.

%% TODO
%% + test for [] for categories
%% + test for [] for projects
%% + test for [] for archives

index(A, PhasedVars) ->
  [ 
      {ewc, blog_navigator, projects, [A]}
    , {ewc, blog_navigator, categories, [A]} 
    , {ewc, blog_navigator, last_entries, [A]} 
    , {ewc, blog_navigator, archives, [A]}
    , [ Ewc || {H, Ewc} <- PhasedVars, H =:= html_navigator ]
  ].

categories (A) ->
  Categories = category:find_with({order_by, [ { name, asc } ]})
  , GetName = fun (Row) -> category:name(Row) end
  , LinkPrefix = [ erlyweb:get_app_root(A), <<"blog/categories/">> ]
  , case Categories of
    [] -> {data, <<"No categories found.">>}
    ;_ ->
      Names = [ GetName(C) || C <- Categories ],
      [ {ewc, html_list, hyperlist_item, [ A, [ LinkPrefix, Name ], Name ]} || Name <- Names ]
  end.

%% TODO + recode projects as elegant as categories, they are the same actually
projects (A) ->
  Projects = project:find_with({order_by, [{name,asc}]})
  , GetName = fun (Row) -> project:name(Row) end
  , LinkPrefix = [ erlyweb:get_app_root(A), <<"blog/projects/">> ]
  , case Projects of
    [] -> {data, <<"No projects found.">>}
    ;_ ->
      Names = [ GetName(P) || P <- Projects ],
      [ {ewc, html_list, hyperlist_item, [ A, [ LinkPrefix, Name ], Name ]} || Name <- Names ]
  end.

last_entries(A) ->
  Entries = entry:find({published,'is not',null},[{order_by, {published,desc}}, {limit, ?MAX_LAST_ENTRIES}])
  , GetName = fun (Row) -> entry:title(Row) end
  , GetId = fun (Row) -> entry:id(Row) end
  , LinkPrefix = [ erlyweb:get_app_root(A), <<"blog/entry/">> ]
  , case Entries of
    [] -> {data, <<"No entries found.">>}
    ;_ ->
      Data = [ {GetId(E),GetName(E)} || E <- Entries ],
      [ {ewc, html_list, hyperlist_item, [ A, [LinkPrefix, integer_to_list(Id)], Name ]} || {Id,Name} <- Data ]
  end.
  

archives (A) ->
  Query =
    {esql,
      {select,distinct,[year,month],{from,archive},
      	[{order_by, [{year, desc}, {month, desc}]}]}}
  , F = fun ( [Year, Month] ) ->
      Year_ = integer_to_list(Year)
      , Month_ = integer_to_list(Month)
      , {
          list_to_binary([ erlyweb:get_app_root(A), <<"blog/archives/">>, Year_, $/, Month_ ])
          , list_to_binary([ Year_, $/, Month_ ])
      }
    end
  , {data,{mysql_result, _, Results, _, _}} = 
      erlydb_mysql:q(Query)
  , Archives = [ F(R) || R <- Results ]
  , case Archives of 
    [] -> {data, <<"No archives available.">>}
    ;_ -> [ {ewc, html_list, hyperlist_item, [A, L, Ar]} || {L, Ar} <- Archives ]
  end.
